package cn.edu.swu.miniwebsvr.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Parameter {
    private String method;
    private String url;
    private Map<String, String> urlParams = new HashMap<>();
    private Map<String, String> bodyParams = new HashMap<>();
    private Map<String, String> headers = new HashMap<>();

    // TODO: [222024321072027/舒鑫] 补充必要的构造函数、setter、getter方法
    public Parameter() {
    }

    // URL参数的setter和getter
    public void setUrlParams(Map<String, String> urlParams) {
        this.urlParams = new HashMap<>(urlParams);
    }

    public Map<String, String> getUrlParams() {
        return Collections.unmodifiableMap(urlParams);
    }

    // Body参数的setter和getter
    public void setBodyParams(Map<String, String> bodyParams) {
        this.bodyParams = new HashMap<>(bodyParams);
    }

    public Map<String, String> getBodyParams() {
        return Collections.unmodifiableMap(bodyParams);
    }

    // 添加URL参数
    public void addUrlParam(String name, String value) {
        this.urlParams.put(name, value);
    }

    // 添加Body参数
    public void addBodyParam(String name, String value) {
        this.bodyParams.put(name, value);
    }

    // 添加Header
    public void addHeader(String name, String value) {
        this.headers.put(name, value);
    }

    public Map<String, String> getAllParams() {
        Map<String, String> allParams = new HashMap<>();
        allParams.putAll(urlParams);
        allParams.putAll(bodyParams);
        return Collections.unmodifiableMap(allParams);
    }

    public String getParam(String name) {
        if (bodyParams.containsKey(name)) {
            return bodyParams.get(name);
        }
        return urlParams.get(name);
    }

    // Getter/Setter方法
    public void setMethod(String method) {
        this.method = method;
    }

    public String getMethod() {
        return this.method;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return this.url;
    }

    public Map<String, String> getHeaders() {
        return Collections.unmodifiableMap(headers);
    }

    public String getHeader(String name) {
        return headers.get(name);
    }
}
